package com.simou.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;
import com.simou.config.JacksonObjectMapper;

import java.util.List;
@Slf4j
// 或者 extends WebMvcConfigurerAdapter (已弃用)
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

//    处理静态资源路径
//    静态资源默认是在template、static包下的，此处为自定义资源路径
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/backend/**").addResourceLocations("classpath:backend/");
        registry.addResourceHandler("/front/**").addResourceLocations("classpath:front/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 怎么定制首页后加载出css样式
        // 用请求转发做首页
//        registry.addViewController("/").setViewName("forward:/backend/page/login/login.html");
        registry.addViewController("/").setViewName("redirect:backend/page/login/login.html");
        registry.addViewController("/front").setViewName("redirect:front/page/login.html");
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("消息转换器...");
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        converters.add(0,messageConverter);
    }
}